import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FoodObj extends GameObj {
    private static final int COLUMNS = 60;
    private static final int OFFSET = 10;
    private static final int CELL_SIZE = 30;
    private static final int SCREEN_HEIGHT = 1080;
    private static final int MIN_SIZE = 18;

    // 定义三种食物的最大数量（可独立配置）
    private static final int MAX_SW_QUANTITY = 60;  // 普通食物最大数量
    private static final int MAX_LZ_QUANTITY = 100;  // 龙珠食物最大数量
    private static final int MAX_KX_QUANTITY = 100;  // 扣分食物最大数量

    // 三种食物图片
    private static final Image[] FOOD_IMAGES = {
            GameUtils.swImg,    // 第一种食物图片 (普通)
            GameUtils.lzImg,   // 第二种食物图片 (龙珠)
            GameUtils.kxImg     // 第三种食物图片 (扣分)
    };

    // 当前各类型食物数量计数器
    private static int currentSwCount = 0;
    private static int currentLzCount = 0;
    private static int currentKxCount = 0;

    private String direction = "down";
    private Random r = new Random();
    private int foodType; // 食物类型(0-2)

    public FoodObj() {
        super();
    }

    public FoodObj(Image img, int x, int y, GameWin frame, int foodType) {
        super(img, x, y, frame);
        this.foodType = foodType;
        updateFoodCount(foodType, 1); // 创建时增加对应类型计数
    }

    // 获取单个食物（带数量限制）
    public FoodObj getFood() {
        int type = getRandomFoodTypeWithLimit(); // 考虑数量限制的随机选择
        return new FoodObj(FOOD_IMAGES[type],
                (r.nextInt(COLUMNS) + OFFSET) * CELL_SIZE, 0, this.frame, type);
    }

    // 考虑数量限制的随机食物类型选择
    private int getRandomFoodTypeWithLimit() {
        List<Integer> availableTypes = new ArrayList<>();

        // 检查各类型是否还可以生成
        if (currentSwCount < MAX_SW_QUANTITY) availableTypes.add(0);
        if (currentLzCount < MAX_LZ_QUANTITY) availableTypes.add(1);
        if (currentKxCount < MAX_KX_QUANTITY) availableTypes.add(2);

        // 如果所有类型都达到上限，重置计数器
        if (availableTypes.isEmpty()) {
            resetFoodCounts();
            availableTypes.add(0);
            availableTypes.add(1);
            availableTypes.add(2);
        }

        // 从可用的类型中随机选择
        return availableTypes.get(r.nextInt(availableTypes.size()));
    }

    // 获取三种不同食物的列表（带数量限制）
    public List<FoodObj> getLimitedDifferentFoods() {
        List<FoodObj> foodList = new ArrayList<>();
        List<Integer> typesToGenerate = new ArrayList<>();

        // 根据剩余数量决定要生成哪些类型
        if (currentSwCount < MAX_SW_QUANTITY) typesToGenerate.add(0);
        if (currentLzCount < MAX_LZ_QUANTITY) typesToGenerate.add(1);
        if (currentKxCount < MAX_KX_QUANTITY) typesToGenerate.add(2);

        // 随机打乱顺序
        java.util.Collections.shuffle(typesToGenerate);

        // 最多生成3种（或可生成类型的数量）
        int count = Math.min(50, typesToGenerate.size());
        for (int i = 0; i < count; i++) { int type = typesToGenerate.get(i);
            foodList.add(new FoodObj(FOOD_IMAGES[type], (r.nextInt(COLUMNS) + OFFSET) * CELL_SIZE, -i * CELL_SIZE * 2, this.frame, type));
        }

        return foodList;
    }

    // 更新食物数量计数器
    private void updateFoodCount(int foodType, int delta) {
        switch (foodType) {
            case 0:
                currentSwCount += delta;
                break;
            case 1:
                currentLzCount += delta;
                break;
            case 2:
                currentKxCount += delta;
                break;
        }
    }

    // 重置所有食物计数器
    private void resetFoodCounts() {
        currentSwCount = 0;
        currentLzCount = 0;
        currentKxCount = 0;
    }

    // 当食物移出屏幕时减少计数
    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        move();
        if (y > SCREEN_HEIGHT) {
            updateFoodCount(this.foodType, -1); // 减少旧类型的计数

            y = 0;
            x = (r.nextInt(COLUMNS) + OFFSET) * CELL_SIZE;width = (r.nextInt(3) + 1) * MIN_SIZE;

            // 选择新类型
            this.foodType = getRandomFoodTypeWithLimit();
            this.img = FOOD_IMAGES[foodType];

            updateFoodCount(this.foodType, 1); // 增加新类型的计数
        }
    }

    public void move() {
        y += width;
    }

    public int getFoodType() {
        return foodType;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }
}


